<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>退款单</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/animate.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.min.css?v=4.1.0" rel="stylesheet">
    <style type="text/css">
   	 	#unauditedTab,#auditTab{font-size: 15px;}
    </style>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight" >
    <!-- Panel Other -->
    <div class="ibox float-e-margins" style="background:#fff;">
        <ul class="nav nav-tabs" id="refundOrderTab">
			<li><a href="#unaudited" data-toggle="tab">未审核的退款单</a></li>
			<li><a href="#audit" data-toggle="tab">已审核的退款单</a></li>
		</ul>
		<div id="myTabContent" class="tab-content">
		 <div class="tab-pane fade" id="unaudited">
	    	<div class="ibox-content">
	            <div class="row row-lg">
	                <div class="col-sm-12">
	                    <div class="example-wrap">
	                        <!-- start toolbar -->
	                        <div class="col-sm-12">
	                            <form class="form-horizontal" role="form" id="queryUnauditedForm" autocomplete="off">
	                            	<div class="row">
	                                 	<div class="col-sm-3 col-lg-3">
	                                        <div class="form-group">
	                                            <label class="col-sm-4 control-label">订单号</label>
	                                            <div class="col-sm-8">
	                                                <input type="text" class="form-control" placeholder="订单号"  id="urefundOrderNum" name="refundOrderNum">
	                                            </div>
	                                        </div>
	                                    </div>
	                                    <div class="col-sm-3 col-lg-3">
	                                        <div class="form-group">
	                                            <label class="col-sm-4 control-label">支付编号</label>
	                                            <div class="col-sm-8">
	                                                <input type="text" class="form-control" placeholder="支付编号"  id="urefundPayNum" name="refundPayNum">
	                                            </div>
	                                        </div>
	                                    </div>
		                                <div class="row">
		                                    <div class="col-sm-3 col-lg-2">
		                                    	<button type="button" class="btn btn-primary" id="queryUnauditedBtn">
		                                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询
		                                        </button>
		                                        <button type="button" class="btn btn-info" onclick="unauditedReload()" id="">
		                                            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> 刷新
		                                        </button>
		                                    </div>
		                                </div>
	                                </div>
	                            </form>
	                        </div>
	                        <!-- end toolbar -->
	                        <div class="example">
								<table id="unauditedTab" class="table" data-height="700px" data-click-to-select="true" data-mobile-responsive="true">
								</table>
							</div>
	                    </div>
	                </div>
	            </div>
	        </div>
		    </div>
		    <div class="tab-pane fade" id="audit">
	    	<div class="ibox-content">
	            <div class="row row-lg">
	                <div class="col-sm-12">
	                    <div class="example-wrap">
	                        <!-- start toolbar -->
	                        <div class="col-sm-12">
	                            <form class="form-horizontal" role="form" id="queryAuditForm" autocomplete="off">
	                            	<div class="row">
	                                 	<div class="col-sm-3 col-lg-3">
	                                        <div class="form-group">
	                                            <label class="col-sm-4 control-label">订单号</label>
	                                            <div class="col-sm-8">
	                                                <input type="text" class="form-control" placeholder="订单号" id="arefundOrderNum" name="refundOrderNum">
	                                            </div>
	                                        </div>
	                                    </div>
	                                    <div class="col-sm-3 col-lg-3">
	                                        <div class="form-group">
	                                            <label class="col-sm-4 control-label">支付编号</label>
	                                            <div class="col-sm-8">
	                                                <input type="text" class="form-control" placeholder="支付编号" id="arefundPayNum" name="refundPayNum">
	                                            </div>
	                                        </div>
	                                    </div>
		                                <div class="row">
		                                    <div class="col-sm-4 col-lg-2">
		                                    	<button type="button" class="btn btn-primary" id="queryAuditBtn">
		                                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询
		                                        </button>
		                                        <button type="button" class="btn btn-info" onclick="auditReload()" id="">
		                                            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> 刷新
		                                        </button>
		                                        <button class="btn btn-info" id="refundQueryBtn"  type="button">退款查询</button>
		                                    </div>
		                                </div>
	                                </div>
	                            </form>
	                        </div>
	                        <div style="position:absolute; right:70px; top:60px;">
	                        	
                       		</div>
	                        <!-- end toolbar -->
	                        <div class="example">
								<table id="auditTab" class="table" data-height="700px" data-click-to-select="true" data-mobile-responsive="true">
								</table>
							</div>
	                    </div>
	                </div>
	            </div>
	        </div>
		    </div>
		</div>
    </div>
</div>
<!-- End Panel Other -->
<script src="${pageContext.request.contextPath}/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/js/content.min.js?v=1.0.0"></script>
<script src="${pageContext.request.contextPath}/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="${pageContext.request.contextPath}/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
<script src="${pageContext.request.contextPath}/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="${pageContext.request.contextPath}/js/plugins/layer/layer.min.js"></script>
<script src="${pageContext.request.contextPath}/js/public/ttbj-core.js"></script>
<script type="text/javascript">
var $unauditedTable = $("#unauditedTab");
var $auditTable = $("#auditTab");

$(function () {
    $('#refundOrderTab a').click(function(e) {
        e.preventDefault();//阻止a链接的跳转行为  
        layer.closeAll('tips'); //关闭所有的tips层    
        var whichOne = $(this).attr('href');	//获取是点击的哪个
        if(whichOne == "#unaudited"){
        	initUnauditedTable();
		}else if(whichOne == "#audit") {
			initAuditTable();
		}else {
			layer.msg('信息加载有误，请重新打开', {icon:2});
			return;
		}
    }); 
  	//未审核的退款的条件查询
    $("#queryUnauditedBtn").click(function () {
    	$unauditedTable.bootstrapTable('refresh');	 //使用post提交
    });
  	//已审核的退款的条件查询
    $("#queryAuditBtn").click(function () {
    	$auditTable.bootstrapTable('refresh');	 //使用post提交
    });
  	//触发事件
    $('#refundOrderTab a[href="#unaudited"]').trigger('click');
  	//退款状态查询（微信和支付宝）
    $("#refundQueryBtn").click(function(){
    	var selectContent = $auditTable.bootstrapTable('getSelections')[0];
    	if(typeof(selectContent) == 'undefined') {
        	layer.msg('请选择一条数据!');
            return false;
        }else{
        	var type = selectContent.refundType;
        	var payNum = selectContent.refundPayNum;
        	var code = selectContent.refundCode;
        	var refundNo = selectContent.refundNo;
        	var refundid = selectContent.refundId;
        	$.ajax({
                url: "../zwrefund/zwRefundQuery.htm",
                type: "post",
                dataType: "json",
                data: {'refundType': type,'refundPayNum':payNum, 'refundCode':code, 'outRequestNo':refundNo, 'refundId':refundid},
                success: function (result) {
                    if (result.status == 0) {
                        layer.msg(refundCodeMsg(result.data), {icon: 1});
                        return;
                    }
                    layer.msg(result.message, {icon: 2});
                }
            });
        }
    });
  	
});


//点击未审核刷新按钮
function unauditedReload(){
	$('#refundOrderTab a[href="#unaudited"]').tab('show');
	clearFormData("#queryUnauditedForm");	//清空表单里的查询数据
	$unauditedTable.bootstrapTable('refresh', {url: '../zwrefund/getUnauditedZwRefundInfoByPage.htm'});
}

//点击已审核的刷新按钮
function auditReload(){
	$('#refundOrderTab a[href="#audit"]').tab('show');
	clearFormData("#queryAuditForm");	//清空表单里的查询数据
	$auditTable.bootstrapTable('refresh', {url: '../zwrefund/getAuditZwRefundInfoByPage.htm'});
}

//未审核的table
function initUnauditedTable() {
	$unauditedTable.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryUnauditedParams,
        url: '../zwrefund/getUnauditedZwRefundInfoByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "订单编号",
                field: "refundOrderNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "支付编号",
                field: "refundPayNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "退款金额(元)",
                field: "refundAmt",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "支付方式",
            	field: "refundType",
            	align: "center",
            	formatter: function(value, row, index){
            		var type = row.refundType;
            		if(type == 0){
        				type = "微信";
        			}else if(type == 1){
        				type = "支付宝";
        			}
            		return type;
            	}
            },
            {
                title: "退款原因",
                field: "refundCause",
                align: 'center',
                formatter: function(value, row, index){
            		var cause = row.refundCause;
            		if(cause !='' && cause !=null && cause != undefined){
            			cause = cause.substr(0,30) + '...';
            		}
            		return cause;
            	}
            },
            {
                title:"提交时间",
                field:"refundInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title:"审核状态",
                field:"refundCode",
                align: 'center',
                formatter: function(value, row, index){
            		var code = row.refundCode;
            		if(code == 0){
        				code = "未审核";
        			}
            		return code;
            	}
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var a = '<shiro:hasPermission name="zw:refund"> <button class="btn btn-xs btn-info" type="button" onclick="Audit_Refund(\'' + row.refundId + '\',\'' + (typeof(row.refundType)=="undefined" ? 0 : row.refundType) + '\',\'' + row.refundAmt + '\',\'' + row.refundPayNum + '\',\'' + row.refundOrderNum + '\',\'' + row.refundOrder + '\')"><i class="fa fa-edit"></i>通过</button> </shiro:hasPermission>';
                    var b = '<shiro:hasPermission name="zw:refund"> <button class="btn btn-xs btn-info" type="button" onclick="Audit_Failed(\'' + row.refundId + '\',\'' + row.refundOrderNum + '\',\'' + row.refundOrder +'\')"><i class="fa fa-edit"></i>拒绝</button> </shiro:hasPermission>';
                    return a + b;
                }
            }
        ],
        onClickCell: function(field, value, row, $element){	//当单击某一格，就会触发该事件，- field：点击此格所在列的field，value：点击列的 value 值 
        	if(field == 'refundCause'){
        		layer.tips(value, $element, {
        			  tips: [2, '#3595CC'],	//靠右出现
        			  time: 8000,	//停留多长时间后自动关闭
        			  area: ['500px', '200px']	//宽-高
        		});
        	}
        },
        onClickRow: function(row, $element, field){	//当点击某一行时，该行背景颜色改变
        	var trObj = $($element[0]);
        	var dataIndex = trObj.attr('data-index');
        	$("tr[data-index != '" + dataIndex + "']").removeAttr('style');
        	trObj.attr('style', 'background: #B4EEB4');
        }
    });
}

//退款操作
function Audit_Refund(refundId, refundType, refundAmt, refundPayNum, refundOrderNum, refundOrder){
	console.log('type：' + refundType);
	layer.confirm('确定【' + refundOrderNum + '】此订单的退款申请吗？', {
        btn: ['确定', '取消'] //按钮
    }, function (index) {
    	layer.close(index);	//先关闭confirm询问框
        $.ajax({
            url: "../zwrefund/modifyZwRefundOperation.htm",
            type: "post",
            dataType: "json",
            beforeSend:function(){layer.load(0, {shade:false});},
            complete:function(){layer.closeAll("loading");},
            data: {'refundId': refundId, 'refundType':refundType, 'refundAmt':refundAmt, 'refundPayNum':refundPayNum, 'refundOrder':refundOrder},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    unauditedReload();	//相当于刷线下未审核的订单
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {});
}

//审核拒绝（审核未通过）
function Audit_Failed(refundId, refundOrderNum, refundOrder){
	layer.confirm('确定拒绝【' + refundOrderNum + '】此订单的退款申请吗？', {
        btn: ['确定', '取消'] //按钮
    }, function (index) {
    	layer.close(index);	//先关闭confirm询问框
        $.ajax({
            url: "../zwrefund/modifyRefundAuditFailed.htm",
            cache: false,
            type: "get",
            dataType: "json",
            beforeSend:function(){layer.load(0, {shade:false});},
            complete:function(){layer.closeAll("loading");},
            data: {'refundId': refundId, 'refundOrder':refundOrder},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    unauditedReload();	//相当于刷新下未审核的订单
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {});
}

//已审核的table
function initAuditTable() {
	$auditTable.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        clickToSelect: false,
        queryParams: queryAuditParams,
        url: '../zwrefund/getAuditZwRefundInfoByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
        	{
        		radio: true,
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                	if(row.refundCode ==2 ){
                		return {disabled: true}		//当符合条件时radio才能使用，否则禁用
                	}
                }
            },
        	{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "订单编号",
                field: "refundOrderNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "支付编号",
                field: "refundPayNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "退款金额(元)",
                field: "refundAmt",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "支付方式",
            	field: "refundType",
            	align: "center",
            	formatter: function(value, row, index){
            		var type = row.refundType;
            		if(type == 0){
        				type = "微信";
        			}else{
        				type = "支付宝";
        			}
            		return type;
            	}
            },
            {
                title: "退款原因",
                field: "refundCause",
                align: 'center',
                formatter: function(value, row, index){
            		var cause = row.refundCause;
            		if(cause !='' && cause !=null && cause != undefined){
            			cause = cause.substr(0,30) + '...';
            		}
            		return cause;
            	}
            },
            {
                title:"提交时间",
                field:"refundInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title:"审核状态",
                field:"refundCode",
                align: 'center',
                formatter: function(value, row, index){
            		var code = row.refundCode;
            		if(code == 1){
        				code = "审核通过";
        			}else if(code == 2){
        				code = "审核未通过";
        			}else if(code == 3){
        				code = "退款处理中";
        			}else if(code == 4){
        				code = "退款异常";
        			}else if(code == 5){
        				code = "退款关闭";
        			}
            		return code;
            	}
            },
            {
                title:"退款单号",
                field:"refundNo",
                align: 'center',
                valign: 'middle'
            },
            {
                title:"退款时间",
                field:"refundSuccessTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title:"审核时间",
                field:"refundAuditTime",
                align: 'center',
                valign: 'middle'
            }
        ],
        onClickCell: function(field, value, row, $element){	//当单击某一格，就会触发该事件，- field：点击此格所在列的field，value：点击列的 value 值 
        	if(field == 'refundCause'){
        		layer.tips(value, $element, {
        			  tips: [2, '#3595CC'],	//靠右出现
        			  time: 8000,	//停留多长时间后自动关闭
        			  area: ['500px', '200px']	//宽-高
        		});
        	}
        },
        onClickRow: function(row, $element, field){	//当点击某一行时，该行背景颜色改变
        	var trObj = $($element[0]);
        	var dataIndex = trObj.attr('data-index');
        	$("tr[data-index != '" + dataIndex + "']").removeAttr('style');
        	trObj.attr('style', 'background: #B4EEB4');
        }
    });
}


//分页参数，条件查询时的参数
function queryUnauditedParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,
		offset: queryParamsType.offset,
		refundOrderNum: $.trim($("#urefundOrderNum").val()),
		refundPayNum: $.trim($("#urefundPayNum").val())
	};
	return temp;
}

//分页参数，条件查询时的参数
function queryAuditParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,
		offset: queryParamsType.offset,
		refundOrderNum: $.trim($("#arefundOrderNum").val()),
		refundPayNum: $.trim($("#arefundPayNum").val())
	};
	return temp;
}

//清空表单里的数据
function clearFormData(formId){
	$(':input', formId).not(':button,:submit,:reset,:hidden').val('').removeAttr('checked').removeAttr('selected');
}

//状态码
function refundCodeMsg(data){
	var msg;
	switch (data)
	{
	case 1:
	  	msg="退款成功";
	  	break;
	case 3:
		msg="退款处理中";
	  	break;
	case 4:
		msg="退款异常";
	  	break;
	case 5:
		msg="退款关闭";
	  	break;
	}
	return msg;
}
</script>
</body>
</html>
